package com.guet.ltomanagement.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guet.ltomanagement.entity.SysUnit;
import com.guet.ltomanagement.mapper.SysUnitMapper;
import com.guet.ltomanagement.service.SysUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjj
 * @since 2023-03-03
 */
@Service
public class SysUnitServiceImpl extends ServiceImpl<SysUnitMapper, SysUnit> implements SysUnitService {
    @Autowired
    SysUnitMapper sysUnitMapper;

    @Override
    public Boolean add(SysUnit sysUnit) {
        sysUnit.setCreated(LocalDateTime.now());
        return sysUnitMapper.insert(sysUnit) == 1;
    }

    @Override
    public Boolean update(SysUnit sysUnit) {
        return sysUnitMapper.updateById(sysUnit) == 1;
    }

    @Override
    public Boolean delete(int sysUnitId) {
        return sysUnitMapper.deleteById(sysUnitId) == 1;
    }

    @Override
    public List<SysUnit> getUnitListPage(int page, int size) {
        Page<SysUnit> pages = new Page<>(page,size);
        sysUnitMapper.selectPage(pages, null);
        List<SysUnit> unitList = pages.getRecords();
        return unitList;
    }

    @Override
    public SysUnit getUnitById(Integer id) {
        return sysUnitMapper.selectById(id);
    }

    @Override
    public List<SysUnit> getAllUnit() {
        return sysUnitMapper.selectList(null);
    }

    @Override
    public List<SysUnit> getUnitTree() {
        List<SysUnit> unitList = getAllUnit();
        List<SysUnit> returnList = new ArrayList<SysUnit>();
        //保存所有的UnitId
        List<Long> unitIdList = new ArrayList<Long>();
        for (SysUnit unit : unitList)
        {
            unitIdList.add(unit.getId());
        }
        for (Iterator<SysUnit> iterator = unitList.iterator(); iterator.hasNext();)
        {
            SysUnit unit = (SysUnit) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            // 如果一个部门的父节点不在templist中，说明这个部门在当前数据集中是顶级部门，否则说明该部门是子部门-->
            // 注：不是所有的树都是从root部门，也可能是某个叶子节点遍历的树形结构-->
            if (!unitIdList.contains(unit.getParentId()))
            {
                recursionFn(unitList, unit);
                returnList.add(unit);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = unitList;
        }
        return returnList;
    }

    @Override
    public List<SysUnit> getUnitList() {
        List<SysUnit> sysUnits = sysUnitMapper.selectList(null);
        return sysUnits;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysUnit> list, SysUnit t)
    {
        // 得到子节点列表
        List<SysUnit> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysUnit tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<SysUnit> getChildList(List<SysUnit> list, SysUnit t)
    {
        List<SysUnit> tlist = new ArrayList<SysUnit>();
        Iterator<SysUnit> it = list.iterator();
        while (it.hasNext())
        {
            SysUnit n = (SysUnit) it.next();
            if (n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysUnit> list, SysUnit t)
    {
        return getChildList(list, t).size() > 0;
    }

}
